package com.yitao.cms.controller.product;

import com.skipper.base.bean.DataOutput;
import com.skipper.base.bean.PagedOutput;
import com.yitao.client.constant.ProductConstant;
import com.yitao.client.dto.enums.EnumDto;
import com.yitao.client.dto.product.BaseProductCategoryDto;
import com.yitao.client.dto.product.ProductBrandDto;
import com.yitao.cms.config.aopLog.LogAnnotation;
import com.yitao.cms.dto.product.ProductAddDto;
import com.yitao.cms.dto.product.ProductNameAndSkuDto;
import com.yitao.cms.dto.product.ProductSaleChangeDto;
import com.yitao.cms.service.product.BaseProductCategoryService;
import com.yitao.cms.service.product.ProductBrandServices;
import com.yitao.cms.service.product.ProductCmsService;
import com.yitao.cms.utils.DataUtils;
import com.yitao.cms.vo.product.ProductInfoVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;


/**
 * @author skipper
 * @date 2018-12-17
 * @desc
 * @see
 */
@Api(value = "/cms/productInfo", description = "商品运营后台相关接口", tags = {"cms-product-Info"})
@RestController
@RequestMapping(value = "/productCmsInfo", produces = {"application/json"})
public class ProductCmsInfoController {

    @Autowired
    private ProductCmsService productCmsService;
    @Autowired
    private BaseProductCategoryService baseProductCategoryService;
    @Autowired
    private ProductBrandServices productBrandServices;

    @ApiOperation("新增/编辑商品基本信息")
    @LogAnnotation(targetType = "ProductSave", remark = "新增/编辑商品基本信息")
    @RequestMapping(value = "/saveProductInfoAndImg", method = RequestMethod.POST)
    public DataOutput<ProductAddDto> save( HttpServletRequest request,@RequestBody ProductAddDto productAddDto) {
        return productCmsService.saveProductInfoAndImg(productAddDto, request);
    }

    @ApiOperation("商品列表")
    @RequestMapping(value = "/findProductInfoList", method = RequestMethod.POST)
    public PagedOutput<ProductInfoVo> findProductInfoList(
            HttpServletRequest request,
            @ApiParam(value = "商品Code") @RequestParam(value = "productCode", required = false) String productCode,
            @ApiParam(value = "商品名") @RequestParam(value = "productName", required = false) String productName,
            @ApiParam(value = "开始时间") @RequestParam(value = "staDate", required = false) String staDate,
            @ApiParam(value = "结束时间") @RequestParam(value = "endDate", required = false) String endDate,
            @ApiParam(value = "商品类目") @RequestParam(value = "categoryId", required = false) Long categoryId,
            @ApiParam(value = "是否在售 (0:否，1:是,2：全部)") @RequestParam(value = "isSale", required = false) Integer isSale,
            @ApiParam(value = "分页参数，表示当前页", required = true, defaultValue = "1") @RequestParam(value = "pageNo") Integer pageNo,
            @ApiParam(value = "分页参数，表示每页显示多少条记录", required = true, defaultValue = "10") @RequestParam(value = "pageSize") Integer pageSize
    ) {
        return productCmsService.findProductInfoList(request, DataUtils.Decoder(productName), staDate, endDate, categoryId, isSale, pageNo, pageSize, productCode);
    }

    @ApiOperation("商品列表数据导出")
    @RequestMapping(value = "/findProductInfoExport", method = RequestMethod.GET)
    public void findProductInfoExport(
            HttpServletRequest request, HttpServletResponse response,
            @ApiParam(value = "商品名") @RequestParam(value = "productName", required = false) String productName,
            @ApiParam(value = "开始时间") @RequestParam(value = "staDate", required = false) String staDate,
            @ApiParam(value = "结束时间") @RequestParam(value = "endDate", required = false) String endDate,
            @ApiParam(value = "商品类目") @RequestParam(value = "categoryId", required = false) Long categoryId,
            @ApiParam(value = "是否在售 (0:否，1:是,2：全部)") @RequestParam(value = "isSale", defaultValue = "2") Integer isSale
    ) throws Exception {
        productCmsService.findProductInfoExport(request, response, productName, staDate, endDate, categoryId, isSale);
    }

    @ApiOperation("商品列表-删除")
    @LogAnnotation(targetType = "ProductInfo-Delete", remark = "商品删除")
    @RequestMapping(value = "/deleteProductById", method = RequestMethod.POST)
    public DataOutput deleteProductById(
            HttpServletRequest request,
            @ApiParam(value = "商品Id") @RequestParam(value = "productId") String productId) {
        return productCmsService.deleteProductById(request,productId);
    }

    @ApiOperation("商品列表-下架/上架")
    @LogAnnotation(targetType = "ProductInfo-updateSale", remark = "商品下架/上架")
    @RequestMapping(value = "/updateProductSaleById", method = RequestMethod.POST)
    public DataOutput saleProductById(
            HttpServletRequest request,
            @ApiParam(value = "商品Id") @RequestParam(value = "productId") String productId,
            @ApiParam(value = "是否上架（false:下架，true:上架）") @RequestParam(value = "saleType") Boolean saleType) {
        return productCmsService.updateProductSaleById(request,productId, saleType);
    }

    @ApiOperation("后台根据商品id获取商品详情")
    @RequestMapping(value = "/findProductInfoById", method = RequestMethod.POST)
    public DataOutput<ProductInfoVo> findProductInfoById(@ApiParam(value = "商品id", required = true)
                                                         @RequestParam(value = "productId") Long productId) {
        return productCmsService.findProductInfoById(productId);
    }

    @ApiOperation("获取产品类型枚举")
    @RequestMapping(value = "/getProductTypeEnum", method = RequestMethod.POST)
    public DataOutput<EnumDto> getProductTypeEnum() {
        return new DataOutput(ProductConstant.ProductTypeEnum.getProductTypeEnum());
    }

    @ApiOperation("后台根据商品code获取商品详情")
    @RequestMapping(value = "/findProductInfoByCode", method = RequestMethod.POST)
    public DataOutput<ProductInfoVo> findProductInfoByCode(@ApiParam(value = "商品code", required = true)
                                                           @RequestParam(value = "productCode") String productCode) {
        return productCmsService.findProductInfoByCode(productCode);
    }

    @ApiOperation("后台根据父级id查询基础类型枚举")
    @RequestMapping(value = "/findBaseCategoryByParentId", method = RequestMethod.POST)
    public DataOutput<BaseProductCategoryDto> findBaseCategoryByParentId(
            @ApiParam(value = "父级id", required = true) @RequestParam(value = "parentId", defaultValue = "0") Long parentId) {
        return baseProductCategoryService.findBaseCategoryByParentId(parentId);
    }

    @ApiOperation("后台查询品牌列表")
    @RequestMapping(value = "/findProductBrandList", method = RequestMethod.POST)
    public DataOutput<ProductBrandDto> findProductBrandList(
            @ApiParam(value = "品牌码") @RequestParam(value = "brandCode", required = false) String brandCode,
            @ApiParam(value = "中文名") @RequestParam(value = "cnName", required = false) String cnName,
            @ApiParam(value = "英文名") @RequestParam(value = "enName", required = false) String enName) {
        return productBrandServices.findProductBrandList(brandCode, cnName, enName);
    }


    @ApiOperation("商品-批量下架/上架")
    @LogAnnotation(targetType = "ProductInfo-updateSaleStatusByIds", remark = "商品批量下架/上架")
    @RequestMapping(value = "/updateSaleStatusByIds", method = RequestMethod.POST)
    public DataOutput updateSaleStatusByIds(
           HttpServletRequest request, @RequestBody ProductSaleChangeDto productSaleChangeDto) {
        return productCmsService.updateSaleStatusByIds( request,productSaleChangeDto);
    }


    @ApiOperation("商品-上架中商品修改商品名及价格(v1.2.3)")
    @RequestMapping(value = "/updateProductNameAndSku", method = RequestMethod.POST)
    public DataOutput updateProductNameAndSku(
            HttpServletRequest request, @RequestBody ProductNameAndSkuDto productNameAndSkuDto) {
        return productCmsService.updateProductNameAndSku( request,productNameAndSkuDto);
    }
    @ApiOperation("商品列表-批量删除(v1.2.3)")
    @LogAnnotation(targetType = "ProductInfoByIdS-Delete", remark = "商品批量删除")
    @RequestMapping(value = "/deleteProductByIdS", method = RequestMethod.POST)
    public DataOutput deleteProductByIdS(
            HttpServletRequest request,
            @ApiParam(value = "商品Id集合") @RequestParam(value = "productIdList")List<Long> productIdList) {
        return productCmsService.deleteProductByIdS(request,productIdList);
    }
    @ApiOperation("修改商品排序值")
    @RequestMapping(value = "/updateProductSortById", method = RequestMethod.POST)
    public DataOutput updateProductSortById(
            HttpServletRequest request,
            @ApiParam(value = "商品Id", required = true) @RequestParam(value = "productId") Long productId,
            @ApiParam(value = "商品排序值", required = true) @RequestParam(value = "productSort") Integer productSort) {
        return productCmsService.updateProductSortById( request,productId,productSort);
    }


}
